package com.rsa107.cahce;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.Configuration;
import javax.cache.configuration.MutableConfiguration;

/**
 * RSA 107规约，缓存
 * @author yanyuan
 */

@Slf4j
public class CacheUtils {

    static Cache<String, String> cache;

    static {
        init();
    }

    public CacheUtils() {
    }

    public static void init() {
        //创建一个缓存管理器
        CacheManager manager = Caching.getCachingProvider().getCacheManager();
        //创建一个配置管理器
        Configuration<String, String> configuration = new MutableConfiguration();
        //生成一个缓存对象
        cache = manager.createCache("cache01", configuration);
    }

    /**
     * 缓存数据
     * @param key
     * @param value
     */
    public static void put(String key, String value) {
        cache.put(key, value);
    }

    /**
     * 获取数据
     * @param key
     * @return
     */
    public static String get(String key) {
        return cache.get(key);
    }

    public static void main(String[] args) {
        put("key01", "value01");
        put("key02", "value02");
        put("key03", "value03");
        put("key04", "value04");
        String findKey = "key01";
        log.info("cache get key: {}, value : {}", findKey, get(findKey));
        findKey = "key011";
        log.info("cache get key: {}, value : {}", findKey, get(findKey));
    }
}

